package project.layer.physical;

import project.events.packageEvents.PackagePickedEvent;
import project.events.truckEvents.TruckCreatedEvent;
import project.layer.agents.PickUpPointAgent;
import project.layer.agents.DropOffPointAgent;
import framework.events.EventBroker;
import framework.layer.physical.command.Command;
import framework.layer.physical.entities.vehicles.Vehicle;

public class Truck extends Vehicle<Truck, Crossroad, Road>{
	
	private Package pdpPackage;
		
	public Truck(double speed) {
		this.setSpeed(speed);
		EventBroker.getEventBroker().notifyAll(new TruckCreatedEvent(this));
	}

	public Package getPackage(){
		return pdpPackage;
	}
	private void setPackage(Package pdpPackage){
		this.pdpPackage = pdpPackage;
	}
	public boolean hasPackage(){
		return pdpPackage != null;
	}
	
	public void deliverPackage(){
		if(getPosition().isOnConnector() && hasPackage()){
			Crossroad cr = getConnectorPosition().getConnector();
			if(cr.hasDropOffPoint()){
				DropOffPointAgent dropOffPointAgent = (DropOffPointAgent) cr.getDropOffPoint().getCorrespondingAgent();
				if(dropOffPointAgent.acceptPackage(getPackage())){
					setPackage(null);
				}
			}
		}
	}
	public void pickUpPackage(){
		if(getPosition().isOnConnector() && !hasPackage()){
			Crossroad cr = getConnectorPosition().getConnector();
			if(cr.hasPickUpPoint()){
				PickUpPointAgent pickUpPointAgent = (PickUpPointAgent)cr.getPickUpPoint().getCorrespondingAgent();
				
				if(pickUpPointAgent.hasPackage()){
					setPackage(pickUpPointAgent.pickUpPackage());
				}
			}
		}
	}

	@Override
	protected Command<? extends Truck> loadFailSafeCommand() {
		// TODO Auto-generated method stub
		return null;
	}
	
}
